home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / symbols.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-07  |  59.0 KB  |  2,058 lines

  1. /* "intern" and friends -- moved here from lread.c
  2.    Copyright (C) 1985-1989, 1992-1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: FSF 19.28. */
  21.  
  22. /* This file has been Mule-ized. */
  23.  
  24. /* #### Ugh, though, this file does awful things with symbol-value-magic
  25.    objects.  This ought to be cleaned up. */
  26.  
  27. #include <config.h>
  28. #include "lisp.h"
  29. #include "symeval.h"
  30. #include "buffer.h"        /* for Vbuffer_defaults */
  31.  
  32. Lisp_Object Qad_advice_info, Qad_activate;
  33.  
  34.  
  35. #ifdef LRECORD_SYMBOL
  36.  
  37. static Lisp_Object mark_symbol (Lisp_Object, void (*) (Lisp_Object));
  38. extern void print_symbol (Lisp_Object, Lisp_Object, int);
  39. DEFINE_LRECORD_IMPLEMENTATION ("symbol", symbol,
  40.                    mark_symbol, print_symbol, 0, 0, 0,
  41.                    struct Lisp_Symbol);
  42.  
  43. static Lisp_Object
  44. mark_symbol (Lisp_Object obj, void (*markobj) (Lisp_Object))
  45. {
  46.   struct Lisp_Symbol *sym = XSYMBOL (obj);
  47.   Lisp_Object pname;
  48.  
  49.   ((markobj) (sym->value));
  50.   ((markobj) (sym->function));
  51.   XSETSTRING (pname, sym->name);
  52.   ((markobj) (pname));
  53.   if (!symbol_next (sym))
  54.     return (sym->plist);
  55.   else
  56.   {
  57.     ((markobj) (sym->plist));
  58.     /* Mark the rest of the symbols in the obarray hash-chain */
  59.     sym = symbol_next (sym);
  60.     XSETSYMBOL (obj, sym);
  61.     return (obj);
  62.   }
  63. }
  64.  
  65. #endif /* LRECORD_SYMBOL */
  66.  
  67.  
  68. /**********************************************************************/
  69. /* Intern                                  */
  70. /**********************************************************************/
  71.  
  72. Lisp_Object Vobarray;
  73.  
  74. static Lisp_Object initial_obarray;
  75.  
  76. static Lisp_Object
  77. check_obarray (Lisp_Object obarray)
  78. {
  79.   while (!VECTORP (obarray) || vector_length (XVECTOR (obarray)) == 0)
  80.     {
  81.       /* If Vobarray is now invalid, force it to be valid.  */
  82.       if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
  83.  
  84.       obarray = wrong_type_argument (Qvectorp, obarray);
  85.     }
  86.   return obarray;
  87. }
  88.  
  89. Lisp_Object
  90. intern (CONST char *str)
  91. {
  92.   Lisp_Object tem;
  93.   Bytecount len = strlen (str);
  94.   Lisp_Object obarray = Vobarray;
  95.   if (!VECTORP (obarray) || vector_length (XVECTOR (obarray)) == 0)
  96.     obarray = check_obarray (obarray);
  97.   tem = oblookup (obarray, (CONST Bufbyte *) str, len);
  98.     
  99.   if (SYMBOLP (tem))
  100.     return tem;
  101.   return Fintern (((purify_flag)
  102.            ? make_pure_pname ((Bufbyte *) str, len, 0)
  103.            : make_string ((Bufbyte *) str, len)),
  104.           obarray);
  105. }
  106.  
  107. DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
  108.   "Return the canonical symbol whose name is STRING.\n\
  109. If there is none, one is created by this function and returned.\n\
  110. A second optional argument specifies the obarray to use;\n\
  111. it defaults to the value of `obarray'.")
  112.   (str, obarray)
  113.      Lisp_Object str, obarray;
  114. {
  115.   Lisp_Object sym, *ptr;
  116.   Bytecount len;
  117.  
  118.   if (NILP (obarray)) obarray = Vobarray;
  119.   obarray = check_obarray (obarray);
  120.  
  121.   CHECK_STRING (str, 0);
  122.  
  123.   len = string_length (XSTRING (str));
  124.   sym = oblookup (obarray, string_data (XSTRING (str)), len);
  125.   if (!INTP (sym))
  126.     /* Found it */
  127.     return sym;
  128.  
  129.   ptr = &vector_data (XVECTOR (obarray))[XINT (sym)];
  130.  
  131.   if (purify_flag && ! purified (str))
  132.     str = make_pure_pname (string_data (XSTRING (str)), len, 0);
  133.   sym = Fmake_symbol (str);
  134.  
  135.   if (SYMBOLP (*ptr))
  136.     symbol_next (XSYMBOL (sym)) = XSYMBOL (*ptr);
  137.   else
  138.     symbol_next (XSYMBOL (sym)) = 0;
  139.   *ptr = sym;
  140.   return sym;
  141. }
  142.  
  143. DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
  144.   "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
  145. A second optional argument specifies the obarray to use;\n\
  146. it defaults to the value of `obarray'.")
  147.   (str, obarray)
  148.      Lisp_Object str, obarray;
  149. {
  150.   Lisp_Object tem;
  151.  
  152.   if (NILP (obarray)) obarray = Vobarray;
  153.   obarray = check_obarray (obarray);
  154.  
  155.   CHECK_STRING (str, 0);
  156.  
  157.   tem = oblookup (obarray, string_data (XSTRING (str)),
  158.           string_length (XSTRING (str)));
  159.   if (!INTP (tem))
  160.     return tem;
  161.   return Qnil;
  162. }
  163.  
  164. Lisp_Object
  165. oblookup (Lisp_Object obarray, CONST Bufbyte *ptr, Bytecount size)
  166. {
  167.   int hash, obsize;
  168.   struct Lisp_Symbol *tail;
  169.   Lisp_Object bucket;
  170.  
  171.   while (!VECTORP (obarray) ||
  172.      (obsize = vector_length (XVECTOR (obarray))) == 0)
  173.     {
  174.       obarray = check_obarray (obarray);
  175.     }
  176.   /* Combining next two lines breaks VMS C 2.3.     */
  177.   hash = hash_string (ptr, size);
  178.   hash %= obsize;
  179.   bucket = vector_data (XVECTOR (obarray))[hash];
  180.   if (XINT (bucket) == 0)
  181.     ;
  182.   else if (!SYMBOLP (bucket))
  183.     error ("Bad data in guts of obarray"); /* Like CADR error message */
  184.   else
  185.     for (tail = XSYMBOL (bucket); ;)
  186.       {
  187.     if (string_length (tail->name) == size &&
  188.         !memcmp (string_data (tail->name), ptr, size))
  189.       {
  190.         XSETSYMBOL (bucket, tail);
  191.         return (bucket);
  192.       }
  193.     tail = symbol_next (tail);
  194.     if (!tail)
  195.       break;
  196.       }
  197.   return (make_number (hash));
  198. }
  199.  
  200. int
  201. hash_string (CONST Bufbyte *ptr, Bytecount len)
  202. {
  203.   CONST Bufbyte *p = ptr;
  204.   CONST Bufbyte *end = p + len;
  205.   Bufbyte c;
  206.   int hash = 0;
  207.  
  208.   while (p != end)
  209.     {
  210.       c = *p++;
  211.       if (c >= 0140) c -= 40;
  212.       hash = ((hash<<3) + (hash>>28) + c);
  213.     }
  214.   return hash & 07777777777;
  215. }
  216.  
  217. void
  218. map_obarray (Lisp_Object obarray,
  219.          void (*fn) (Lisp_Object sym, Lisp_Object arg),
  220.          Lisp_Object arg)
  221. {
  222.   int i;
  223.   Lisp_Object tail;
  224.   CHECK_VECTOR (obarray, 1);
  225.   for (i = vector_length (XVECTOR (obarray)) - 1; i >= 0; i--)
  226.     {
  227.       tail = vector_data (XVECTOR (obarray))[i];
  228.       if (SYMBOLP (tail))
  229.     while (1)
  230.       {
  231.         struct Lisp_Symbol *next;
  232.         (*fn) (tail, arg);
  233.         next = symbol_next (XSYMBOL (tail));
  234.         if (!next)
  235.           break;
  236.         XSETSYMBOL (tail, next);
  237.       }
  238.     }
  239. }
  240.  
  241. static void
  242. mapatoms_1 (Lisp_Object sym, Lisp_Object function)
  243. {
  244.   call1 (function, sym);
  245. }
  246.  
  247. DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
  248.   "Call FUNCTION on every symbol in OBARRAY.\n\
  249. OBARRAY defaults to the value of `obarray'.")
  250.   (function, obarray)
  251.      Lisp_Object function, obarray;
  252. {
  253.   if (NILP (obarray))
  254.     obarray = Vobarray;
  255.   obarray = check_obarray (obarray);
  256.  
  257.   map_obarray (obarray, mapatoms_1, function);
  258.   return Qnil;
  259. }
  260.  
  261.  
  262. /**********************************************************************/
  263. /* Apropos                                  */
  264. /**********************************************************************/
  265.  
  266. static void
  267. apropos_accum (Lisp_Object symbol, Lisp_Object arg)
  268. {
  269.   Lisp_Object tem;
  270.   Lisp_Object string = XCAR (arg);
  271.   Lisp_Object predicate = XCAR (XCDR (arg));
  272.   Lisp_Object *accumulation = &(XCDR (XCDR (arg)));
  273.  
  274.   tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
  275.   if (!NILP (tem) && !NILP (predicate))
  276.     tem = call1 (predicate, symbol);
  277.   if (!NILP (tem))
  278.     *accumulation = Fcons (symbol, *accumulation);
  279. }
  280.  
  281. DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0, 
  282.   "Show all symbols whose names contain match for REGEXP.\n\
  283. If optional 2nd arg PRED is non-nil, (funcall PRED SYM) is done\n\
  284. for each symbol and a symbol is mentioned only if that returns non-nil.\n\
  285. Return list of symbols found.")
  286.   (string, pred)
  287.      Lisp_Object string, pred;
  288. {
  289.   struct gcpro gcpro1;
  290.   Lisp_Object accumulation;
  291.  
  292.   CHECK_STRING (string, 0);
  293.   accumulation = Fcons (string, Fcons (pred, Qnil));
  294.   GCPRO1 (accumulation);
  295.   map_obarray (Vobarray, apropos_accum, accumulation);
  296.   accumulation = Fsort (Fcdr (Fcdr (accumulation)), Qstring_lessp);
  297.   UNGCPRO;
  298.   return (accumulation);
  299. }
  300.  
  301.  
  302. /* Extract and set components of symbols */
  303.  
  304. static Lisp_Object swap_in_symval_forwarding
  305.             (Lisp_Object sym, struct symbol_value_buffer_local *);
  306.  
  307. DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0, "T if SYMBOL's value is not void.")
  308.   (sym)
  309.      Lisp_Object sym;
  310. {
  311.   CHECK_SYMBOL (sym, 0);
  312.   return (EQ (find_symbol_value (sym), Qunbound) ? Qnil : Qt);
  313. }
  314.  
  315. DEFUN ("globally-boundp", Fglobally_boundp, Sglobally_boundp, 1, 1, 0,
  316.        "T if SYMBOL has a global (non-bound) value.\n\
  317. This is for the byte-compiler; you really shouldn't be using this.")
  318.   (sym)
  319.      Lisp_Object sym;
  320. {
  321.   CHECK_SYMBOL (sym, 0);
  322.   return (EQ (top_level_value (sym), Qunbound) ? Qnil : Qt);
  323. }
  324.  
  325. DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
  326.        "T if SYMBOL's function definition is not void.")
  327.   (sym)
  328.      Lisp_Object sym;
  329. {
  330.   CHECK_SYMBOL (sym, 0);
  331.   return ((EQ (XSYMBOL (sym)->function, Qunbound)) ? Qnil : Qt);
  332. }
  333.  
  334. static void
  335. reject_constant_symbols (Lisp_Object sym, Lisp_Object newval, int function_p)
  336. {
  337.   /* #### - I wonder if it would be better to just have a new magic value
  338.      type and make nil, t, and all keywords have that same magic
  339.      constant_symbol value.  This test is awfully specific about what is
  340.      constant and what isn't.  --Stig */
  341.   if (NILP (sym) || EQ (sym, Qt)
  342. #if 0
  343.       /* #### - This is disabled until a new magic symbol_value for
  344.      constants is added */
  345.       || SYMBOL_IS_KEYWORD (sym)
  346. #endif
  347.       )
  348.     {
  349.       signal_error (Qsetting_constant,
  350.             ((EQ (newval, Qunbound))
  351.              ? list1 (sym)
  352.              : list2 (sym, newval)));
  353.     }
  354.  
  355.   if (SYMBOL_VALUE_MAGIC_P (XSYMBOL (sym)->value) &&
  356.       XSYMBOL_VALUE_MAGIC_TYPE (XSYMBOL (sym)->value) ==
  357.       SYMVAL_CONST_SPECIFIER_FORWARD)
  358.     signal_simple_error ("Use `set-specifier' to change a specifier's value",
  359.              sym);
  360. }
  361.  
  362. static void
  363. verify_ok_for_buffer_local (Lisp_Object sym)
  364. {
  365.   if (NILP (sym) || EQ (sym, Qt)
  366. #if 0
  367.       /* #### - This is disabled until a new magic symbol_value for
  368.      constants is added */
  369.       || SYMBOL_IS_KEYWORD (sym)
  370. #endif
  371.       || (SYMBOL_VALUE_MAGIC_P (XSYMBOL (sym)->value) &&
  372.       XSYMBOL_VALUE_MAGIC_TYPE (XSYMBOL (sym)->value) ==
  373.       SYMVAL_DEFAULT_BUFFER_FORWARD)
  374.       || (SYMBOL_VALUE_MAGIC_P (XSYMBOL (sym)->value) &&
  375.       XSYMBOL_VALUE_MAGIC_TYPE (XSYMBOL (sym)->value) ==
  376.       SYMVAL_CONST_SPECIFIER_FORWARD))
  377.     signal_error (Qerror, 
  378.           list2 (build_string ("Symbol may not be buffer-local"),
  379.              sym));
  380. }
  381.  
  382. DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
  383.        "Make SYMBOL's value be void.")
  384.   (sym)
  385.      Lisp_Object sym;
  386. {
  387.   CHECK_SYMBOL (sym, 0);
  388.   reject_constant_symbols (sym, Qunbound, 0);
  389.   Fset (sym, Qunbound);
  390.   return sym;
  391. }
  392.  
  393. DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
  394.        "Make SYMBOL's function definition be void.")
  395.   (sym)
  396.      Lisp_Object sym;
  397. {
  398.   CHECK_SYMBOL (sym, 0);
  399.   reject_constant_symbols (sym, Qunbound, 1);
  400.   XSYMBOL (sym)->function = Qunbound;
  401.   return sym;
  402. }
  403.  
  404. DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
  405.   "Return SYMBOL's function definition.     Error if that is void.")
  406.   (symbol)
  407.      Lisp_Object symbol;
  408. {
  409.   CHECK_SYMBOL (symbol, 0);
  410.   if (EQ (XSYMBOL (symbol)->function, Qunbound))
  411.     return Fsignal (Qvoid_function, list1 (symbol));
  412.   return XSYMBOL (symbol)->function;
  413. }
  414.  
  415. DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
  416.        "Return SYMBOL's property list.")
  417.   (sym)
  418.      Lisp_Object sym;
  419. {
  420.   CHECK_SYMBOL (sym, 0);
  421.   return XSYMBOL (sym)->plist;
  422. }
  423.  
  424. DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
  425.        "Return SYMBOL's name, a string.")
  426.   (sym)
  427.      Lisp_Object sym;
  428. {
  429.   Lisp_Object name;
  430.  
  431.   CHECK_SYMBOL (sym, 0);
  432.   XSETSTRING (name, XSYMBOL (sym)->name);
  433.   return name;
  434. }
  435.  
  436. DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
  437.   "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.")
  438.   (sym, newdef)
  439.      Lisp_Object sym, newdef;
  440. {
  441.   /* This function can GC */
  442.   CHECK_SYMBOL (sym, 0);
  443.   reject_constant_symbols (sym, newdef, 1);
  444.   if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (sym)->function, Qunbound))
  445.     Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
  446.                  Vautoload_queue);
  447.   XSYMBOL (sym)->function = newdef;
  448.   /* Handle automatic advice activation */
  449.   if (CONSP (XSYMBOL (sym)->plist) && !NILP (Fget (sym, Qad_advice_info,
  450.                              Qnil)))
  451.     {
  452.       call2 (Qad_activate, sym, Qnil);
  453.       newdef = XSYMBOL (sym)->function;
  454.     }
  455.   return newdef;
  456. }
  457.  
  458. /* FSFmacs */
  459. DEFUN ("define-function", Fdefine_function, Sdefine_function, 2, 2, 0,
  460.   "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.\n\
  461. Associates the function with the current load file, if any.")
  462.   (sym, newdef)
  463.      Lisp_Object sym, newdef;
  464. {
  465.   /* This function can GC */
  466.   CHECK_SYMBOL (sym, 0);
  467.   Ffset (sym, newdef);
  468.   LOADHIST_ATTACH (sym);
  469.   return newdef;
  470. }
  471.  
  472.  
  473. DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
  474.   "Set SYMBOL's property list to NEWVAL, and return NEWVAL.")
  475.   (sym, newplist)
  476.      Lisp_Object sym, newplist;
  477. {
  478.   CHECK_SYMBOL (sym, 0);
  479.   XSYMBOL (sym)->plist = newplist;
  480.   return newplist;
  481. }
  482.  
  483.  
  484. /**********************************************************************/
  485. /* symbol-value                                  */
  486. /**********************************************************************/
  487.  
  488. /* If the contents of the value cell of a symbol is one of the following
  489.    three types of objects, then the symbol is "magic" in that setting
  490.    and retrieving its value doesn't just set or retrieve the raw
  491.    contents of the value cell.  None of these objects can escape to
  492.    the user level, so there is no loss of generality.
  493.  
  494.    If a symbol is "unbound", then the contents of its value cell is
  495.    Qunbound.  Despite appearances, this is *not* a symbol, but is
  496.    a symbol-value-forward object.
  497.  
  498.    Logically all of the following objects are "symbol-value-magic"
  499.    objects, and there are some games played w.r.t. this (#### this
  500.    should be cleaned up).  SYMBOL_VALUE_MAGIC_P is true for all of
  501.    the object types.  XSYMBOL_VALUE_MAGIC_TYPE returns the type of
  502.    symbol-value-magic object.  There are more than three types
  503.    returned by this macro: in particular, symbol-value-forward
  504.    has six subtypes, and symbol-value-buffer-local has two.  See
  505.    symeval.h.
  506.  
  507.    symbol-value-forward is used for variables whose actual contents
  508.    are stored in a C variable of some sort, and for Qunbound.  The
  509.    lcheader.next field (which is only used to chain together free
  510.    lcrecords) holds a pointer to the actual C variable.  Included
  511.    in this type are "buffer-local" variables that are actually
  512.    stored in the buffer object itself; in this case, the "pointer"
  513.    is an offset into the struct buffer structure.
  514.  
  515.    symbol-value-buffer-local is used for variables that have had
  516.    `make-local-variable' or `make-variable-buffer-local' applied
  517.    to them.  This object contains an alist mapping buffers to
  518.    values.  In addition, the object contains a "current value",
  519.    which is the value in some buffer.  Whenever you access the
  520.    variable with `symbol-value' or set it with `set' or `setq',
  521.    things are switched around so that the "current value"
  522.    refers to the current buffer, if it wasn't already.  This
  523.    way, repeated references to a variable in the same buffer
  524.    are almost as efficient as if the variable weren't buffer
  525.    local.  Note that the alist may not be up-to-date w.r.t.
  526.    the buffer whose value is current, as the "current value"
  527.    cache is normally only flushed into the alist when the
  528.    buffer it refers to changes.
  529.  
  530.    Note also that it is possible for `make-local-variable'
  531.    or `make-variable-buffer-local' to be called on a variable
  532.    that forwards into a C variable (i.e. a variable whose
  533.    value cell is a symbol-value-forward).  In this case,
  534.    the value cell becomes a symbol-value-buffer-local (as
  535.    always), and the symbol-value-forward moves into
  536.    the "current value" cell in this object.  Also, in
  537.    this case the "current value" *always* refers to the
  538.    current buffer, so that the values of the C variable
  539.    always is the correct value for the current buffer.
  540.    set_buffer_internal() automatically updates the current-value
  541.    cells of all buffer-local variables that forward into C
  542.    variables. (There is a list of all buffer-local variables
  543.    that is maintained for this and other purposes.)
  544.  
  545.    A symbol-value-varalias object is used for variables that
  546.    are aliases for other variables.  This object contains
  547.    the symbol that this variable is aliased to.
  548.    symbol-value-varalias objects cannot occur anywhere within
  549.    a symbol-value-buffer-local object, and most of the
  550.    low-level functions below do not accept them; you need
  551.    to call follow_varalias_pointers to get the actual
  552.    symbol to operate on.
  553.    */
  554.  
  555. static Lisp_Object mark_symbol_value_buffer_local (Lisp_Object,
  556.                            void (*) (Lisp_Object));
  557. static Lisp_Object mark_symbol_value_varalias (Lisp_Object,
  558.                            void (*) (Lisp_Object));
  559.  
  560. DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-forward",
  561.                    symbol_value_forward,
  562.                    this_one_is_unmarkable,
  563.                    print_symbol_value_magic, 0, 0, 0,
  564.                    struct symbol_value_forward);
  565.  
  566. DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-buffer-local",
  567.                    symbol_value_buffer_local, 
  568.                    mark_symbol_value_buffer_local,
  569.                    print_symbol_value_magic, 
  570.                    0, 0, 0,
  571.                    struct symbol_value_buffer_local);
  572.  
  573. DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-varalias",
  574.                    symbol_value_varalias, 
  575.                    mark_symbol_value_varalias,
  576.                    print_symbol_value_magic, 
  577.                    0, 0, 0,
  578.                    struct symbol_value_varalias);
  579.  
  580. static Lisp_Object
  581. mark_symbol_value_buffer_local (Lisp_Object obj,
  582.                 void (*markobj) (Lisp_Object))
  583. {
  584.   struct symbol_value_buffer_local *bfwd;
  585.  
  586.   assert (XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_BUFFER_LOCAL ||
  587.       XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_SOME_BUFFER_LOCAL);
  588.  
  589.   bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (obj);
  590.   ((markobj) (bfwd->default_value));
  591.   ((markobj) (bfwd->current_value));
  592.   ((markobj) (bfwd->current_buffer));
  593.   return (bfwd->current_alist_element);
  594. }
  595.  
  596. static Lisp_Object
  597. mark_symbol_value_varalias (Lisp_Object obj,
  598.                 void (*markobj) (Lisp_Object))
  599. {
  600.   struct symbol_value_varalias *bfwd;
  601.  
  602.   assert (XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_VARALIAS);
  603.  
  604.   bfwd = XSYMBOL_VALUE_VARALIAS (obj);
  605.   ((markobj) (bfwd->shadowed));
  606.   return (bfwd->aliasee);
  607. }
  608.  
  609. /* Should never, ever be called. (except by an external debugger) */
  610. void
  611. print_symbol_value_magic (Lisp_Object obj, 
  612.               Lisp_Object printcharfun, int escapeflag)
  613. {
  614.   char buf[200];
  615.   sprintf (buf, "#<INTERNAL EMACS BUG (symfwd %d) 0x%x>",
  616.        (LISP_WORD_TYPE) XSYMBOL_VALUE_MAGIC_TYPE (obj),
  617.        (LISP_WORD_TYPE) XPNTR (obj));
  618.   write_c_string (buf, printcharfun);
  619. }
  620.  
  621.  
  622. /* Getting and setting values of symbols */
  623.  
  624. /* Given the raw contents of a symbol value cell, return the Lisp value of
  625.    the symbol.  The raw contents cannot be a symbol-value-buffer-local or
  626.    symbol-value-varalias; use other functions for that.
  627.  */
  628.  
  629. static Lisp_Object
  630. do_symval_forwarding (Lisp_Object valcontents, struct buffer *buffer)
  631. {
  632.   CONST struct symbol_value_forward *fwd;
  633.  
  634.   if (!SYMBOL_VALUE_MAGIC_P (valcontents))
  635.     return (valcontents);
  636.  
  637.   fwd = XSYMBOL_VALUE_FORWARD (valcontents);
  638.   switch (fwd->magic.type)
  639.     {
  640.     case SYMVAL_FIXNUM_FORWARD:
  641.       return (make_number (*((int *)symbol_value_forward_forward (fwd))));
  642.  
  643.     case SYMVAL_BOOLEAN_FORWARD:
  644.       {
  645.     if (*((int *)symbol_value_forward_forward (fwd)))
  646.       return (Qt);
  647.     else
  648.       return (Qnil);
  649.       }
  650.  
  651.     case SYMVAL_OBJECT_FORWARD:
  652.     case SYMVAL_CONST_SPECIFIER_FORWARD:
  653.       return (*((Lisp_Object *)symbol_value_forward_forward (fwd)));
  654.  
  655.     case SYMVAL_DEFAULT_BUFFER_FORWARD:
  656.       return (*((Lisp_Object *)((char *) XBUFFER (Vbuffer_defaults)
  657.                 + ((char *)symbol_value_forward_forward (fwd)
  658.                    - (char *)&buffer_local_flags))));
  659.  
  660.  
  661.     case SYMVAL_CURRENT_BUFFER_FORWARD:
  662.       return (*((Lisp_Object *)((char *)buffer
  663.                 + ((char *)symbol_value_forward_forward (fwd)
  664.                    - (char *)&buffer_local_flags))));
  665.  
  666.     case SYMVAL_UNBOUND_MARKER:
  667.       return (valcontents);
  668.  
  669. #ifdef LISP_MAGIC
  670.     case SYMVAL_LISP_MAGIC:
  671. #endif 
  672.  
  673.     default:
  674.       abort ();
  675.     }
  676.   return Qnil;    /* suppress compiler warning */
  677. }
  678.  
  679. static void
  680. set_default_buffer_slot_variable (Lisp_Object sym,
  681.                   Lisp_Object value)
  682. {
  683.   /* Handle variables like case-fold-search that have special slots in
  684.      the buffer. Make them work apparently like buffer_local variables.
  685.      */
  686.   Lisp_Object valcontents = XSYMBOL (sym)->value;
  687.   CONST struct symbol_value_forward *fwd 
  688.     = XSYMBOL_VALUE_FORWARD (valcontents);
  689.   int offset = ((char *) symbol_value_forward_forward (fwd) 
  690.         - (char *) &buffer_local_flags);
  691.   int mask = XINT (*((Lisp_Object *) symbol_value_forward_forward (fwd)));
  692.   int (*magicfun) (Lisp_Object sym, Lisp_Object *val, struct buffer *buf,
  693.            int flags) = symbol_value_forward_magicfun (fwd);
  694.   
  695.   *((Lisp_Object *) (offset + (char *) XBUFFER (Vbuffer_defaults)))
  696.     = value;
  697.   
  698.   if (mask > 0)        /* Not always per-buffer */
  699.     {
  700.       Lisp_Object tail;
  701.       
  702.       /* Set value in each buffer which hasn't shadowed the default */
  703.       LIST_LOOP (tail, Vbuffer_alist)
  704.     {
  705.       struct buffer *b = XBUFFER (XCDR (XCAR (tail)));
  706.       if (!(b->local_var_flags & mask))
  707.         {
  708.           if (magicfun)
  709.         (magicfun) (sym, &value, b, 0);
  710.           *((Lisp_Object *) (offset + (char *) b)) = value; 
  711.         }
  712.     }
  713.     }
  714. }
  715.  
  716. /* Store NEWVAL into SYM.  If SYM is a symbol-value-buffer-local,
  717.    OVALUE should be the contents of the current-value cell in the
  718.    buffer-local structure; otherwise, ovalue should be the actual
  719.    contents of the value cell of SYM.  If SYM is a
  720.    symbol-value-buffer-local, this function will only modify its
  721.    current-value cell, which should already be set up to point to
  722.    the current buffer.  SYM may not be a symbol-value-varalias
  723.    or an unsettable symbol.
  724.   */
  725.  
  726. static void
  727. store_symval_forwarding (Lisp_Object sym, Lisp_Object ovalue,
  728.              Lisp_Object newval)
  729. {
  730.   if (!SYMBOL_VALUE_MAGIC_P (ovalue) || EQ (ovalue, Qunbound))
  731.     {
  732.       ovalue = XSYMBOL (sym)->value;
  733.     
  734.       if (!SYMBOL_VALUE_MAGIC_P (ovalue))
  735.     {
  736.       XSYMBOL (sym)->value = newval;
  737.     }
  738.  
  739.       else switch (XSYMBOL_VALUE_MAGIC_TYPE (ovalue))
  740.     {
  741.     case SYMVAL_BUFFER_LOCAL:
  742.     case SYMVAL_SOME_BUFFER_LOCAL:
  743.       XSYMBOL_VALUE_BUFFER_LOCAL (ovalue)->current_value = newval;
  744.       break;
  745.     default:
  746.       XSYMBOL (sym)->value = newval;
  747.       break;
  748.     }
  749.       return;
  750.     }
  751.  
  752.   else
  753.     {
  754.       CONST struct symbol_value_forward *fwd
  755.     = XSYMBOL_VALUE_FORWARD (ovalue);
  756.       int type = XSYMBOL_VALUE_MAGIC_TYPE (ovalue);
  757.       int (*magicfun) (Lisp_Object sym, Lisp_Object *val, struct buffer *buf,
  758.                int flags) = symbol_value_forward_magicfun (fwd);
  759.  
  760.       switch (type)
  761.     {
  762.     case SYMVAL_FIXNUM_FORWARD:
  763.       {
  764.         CHECK_INT (newval, 1);
  765.         if (magicfun)
  766.           (magicfun) (sym, &newval, 0, 0);
  767.         *((int *) symbol_value_forward_forward (fwd)) = XINT (newval);
  768.         return;
  769.       }
  770.  
  771.     case SYMVAL_BOOLEAN_FORWARD:
  772.       {
  773.         if (magicfun)
  774.           (magicfun) (sym, &newval, 0, 0);
  775.         *((int *) symbol_value_forward_forward (fwd))
  776.           = ((NILP (newval)) ? 0 : 1);
  777.         return;
  778.       }
  779.  
  780.     case SYMVAL_OBJECT_FORWARD:
  781.       {
  782.         if (magicfun)
  783.           (magicfun) (sym, &newval, 0, 0);
  784.         *((Lisp_Object *) symbol_value_forward_forward (fwd)) = newval;
  785.         return;
  786.       }
  787.       
  788.     case SYMVAL_DEFAULT_BUFFER_FORWARD:
  789.       {
  790.         set_default_buffer_slot_variable (sym, newval);
  791.         return;
  792.       }
  793.  
  794.     case SYMVAL_CURRENT_BUFFER_FORWARD:
  795.       {
  796.         if (magicfun)
  797.           (magicfun) (sym, &newval, current_buffer, 0);
  798.         *((Lisp_Object *) ((char *) current_buffer
  799.                    + ((char *) symbol_value_forward_forward (fwd)
  800.                   - (char *) &buffer_local_flags))) 
  801.           = newval;
  802.         return;
  803.       }
  804.  
  805.     default:
  806.       abort ();
  807.     }
  808.     }
  809. }
  810.  
  811. static Lisp_Object
  812. buffer_local_alist_element (struct buffer *buffer, Lisp_Object symbol,
  813.                 struct symbol_value_buffer_local *bfwd)
  814. {
  815.   if (!NILP (bfwd->current_buffer) &&
  816.       XBUFFER (bfwd->current_buffer) == buffer)
  817.     /* This is just an optimization of the below. */
  818.     return (bfwd->current_alist_element);
  819.   else
  820.     return (assq_no_quit (symbol, buffer->local_var_alist));
  821. }
  822.  
  823.  
  824. static Lisp_Object
  825. swap_in_symval_forwarding_1 (Lisp_Object symbol, 
  826.                  struct symbol_value_buffer_local *bfwd)
  827. {
  828.   Lisp_Object cval = do_symval_forwarding (bfwd->current_value,
  829.                        /* was current_buffer */
  830.                        NILP (bfwd->current_buffer) ?
  831.                        0 : XBUFFER (bfwd->current_buffer));
  832.   if (NILP (bfwd->current_alist_element))
  833.     /* current_value may be updated more recently than default_value */
  834.     bfwd->default_value = cval;
  835.   else
  836.     Fsetcdr (bfwd->current_alist_element, cval);
  837.  
  838.   return (buffer_local_alist_element (current_buffer, symbol, bfwd));
  839. }
  840.  
  841.  
  842. /* Set up the buffer-local symbol SYM for validity in the current
  843.    buffer.  BFWD is the contents of its value cell.
  844.    Return the new contents of the current-value cell (this will be
  845.    a symbol-value-forward or a non-magic object). */
  846.  
  847. static Lisp_Object
  848. swap_in_symval_forwarding (Lisp_Object sym, 
  849.                struct symbol_value_buffer_local *bfwd)
  850. {
  851.   /* If the current buffer is not BUFFER, 
  852.      we store the current CURRENT-VALUE value into CURRENT-ALIST-ELEMENT,
  853.      then find the appropriate alist element for the buffer now current
  854.      and set up CURRENT-ALIST-ELEMENT.
  855.      Then we set CURRENT-VALUE out of that element, and store into BUFFER.
  856.      Note that CURRENT-VALUE can be a forwarding pointer. */
  857.   if (NILP (bfwd->current_buffer) ||
  858.       current_buffer != XBUFFER (bfwd->current_buffer))
  859.     {
  860.       Lisp_Object tem = swap_in_symval_forwarding_1 (sym, bfwd);
  861.       if (NILP (tem))
  862.     {
  863.       bfwd->current_alist_element = Qnil;
  864.       tem = bfwd->default_value;
  865.     }
  866.       else
  867.     {
  868.       bfwd->current_alist_element = tem;
  869.       tem = Fcdr (tem);
  870.     }
  871.       XSETBUFFER (bfwd->current_buffer, current_buffer);
  872.       store_symval_forwarding (sym,
  873.                    bfwd->current_value, 
  874.                    tem);
  875.     }
  876.   return (bfwd->current_value);
  877. }
  878.  
  879. /* Follow the chain of variable aliases for OBJECT.  Return the resulting
  880.    symbol, whose value cell is guaranteed not to be a symbol-value-varalias. */
  881.  
  882. static Lisp_Object
  883. follow_varalias_pointers (Lisp_Object object)
  884. {
  885.   Lisp_Object tortoise = object; 
  886.   Lisp_Object hare = object;
  887.  
  888.   /* based off of indirect_function() */
  889.   for (;;)
  890.     {
  891.       Lisp_Object value;
  892.  
  893.       value = XSYMBOL (hare)->value;
  894.       if (!SYMBOL_VALUE_VARALIAS_P (value))
  895.     break;
  896.       hare = symbol_value_varalias_aliasee (XSYMBOL_VALUE_VARALIAS (value));
  897.       value = XSYMBOL (hare)->value;
  898.       if (!SYMBOL_VALUE_VARALIAS_P (value))
  899.     break;
  900.       hare = symbol_value_varalias_aliasee (XSYMBOL_VALUE_VARALIAS (value));
  901.  
  902.       tortoise = symbol_value_varalias_aliasee
  903.     (XSYMBOL_VALUE_VARALIAS (XSYMBOL (tortoise)->value));
  904.  
  905.       if (EQ (hare, tortoise))
  906.     return (Fsignal (Qcyclic_variable_indirection, list1 (object)));
  907.     }
  908.  
  909.   return hare;
  910. }
  911.  
  912.  
  913. void
  914. kill_buffer_local_variables (struct buffer *buf)
  915. {
  916.   Lisp_Object prev;
  917.   Lisp_Object alist;
  918.  
  919.   /* Any which are supposed to be permanent,
  920.      make local again, with the same values they had.  */
  921.      
  922.   for (prev = Qnil, alist = buf->local_var_alist;
  923.        !NILP (alist); 
  924.        prev = alist, alist = XCDR (alist))
  925.     {
  926.       Lisp_Object sym = XCAR (XCAR (alist));
  927.       struct symbol_value_buffer_local *bfwd;
  928.  
  929.       if (!SYMBOL_VALUE_MAGIC_P (XSYMBOL (sym)->value))
  930.     abort ();
  931.  
  932.       bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (XSYMBOL (sym)->value);
  933.       if (bfwd->magic.type != SYMVAL_BUFFER_LOCAL
  934.       && bfwd->magic.type != SYMVAL_SOME_BUFFER_LOCAL)
  935.     abort ();
  936.  
  937.       if (NILP (Fget (sym, Qpermanent_local, Qnil)))
  938.       {
  939.         /* Really truly kill it. */
  940.     if (!NILP (prev))
  941.       XCDR (prev) = XCDR (alist);
  942.     else
  943.       current_buffer->local_var_alist = XCDR (alist);
  944.  
  945.     if (!NILP (bfwd->current_buffer) &&
  946.         buf == XBUFFER (bfwd->current_buffer))
  947.       bfwd->current_buffer = Qnil;
  948.  
  949.     /* In case it's a C variable, flush it out. */
  950.     swap_in_symval_forwarding (sym, bfwd);
  951.       }
  952.     }
  953. }
  954.  
  955. /* Find the value of a symbol, returning Qunbound if it's not bound.
  956.    Note that it must not be possible to QUIT within this function. */
  957.  
  958. Lisp_Object
  959. symbol_value_in_buffer (Lisp_Object sym, Lisp_Object buffer)
  960. {
  961.   Lisp_Object valcontents;
  962.   struct buffer *buf;
  963.  
  964.   CHECK_SYMBOL (sym, 0);
  965.  
  966.  retry:
  967.   CHECK_BUFFER (buffer, 1);
  968.   valcontents = XSYMBOL (sym)->value;
  969.   buf = XBUFFER (buffer);
  970.  
  971.   if (!SYMBOL_VALUE_MAGIC_P (valcontents))
  972.     return (valcontents);
  973.  
  974.   switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
  975.     {
  976.     case SYMVAL_VARALIAS:
  977.       sym = follow_varalias_pointers (sym);
  978.       /* presto change-o! */
  979.       goto retry;
  980.  
  981.     case SYMVAL_BUFFER_LOCAL:
  982.     case SYMVAL_SOME_BUFFER_LOCAL:
  983.       {
  984.     struct symbol_value_buffer_local *bfwd
  985.       = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
  986.  
  987.     if (!NILP (bfwd->current_buffer) &&
  988.         buf == XBUFFER (bfwd->current_buffer))
  989.       valcontents = bfwd->current_value;
  990.     else
  991.       {
  992.         valcontents = assq_no_quit (sym, buf->local_var_alist);
  993.         if (NILP (valcontents))
  994.           valcontents = bfwd->default_value;
  995.         else
  996.           valcontents = Fcdr (valcontents);
  997.       }
  998.     break;
  999.       }
  1000.       
  1001.     default:
  1002.       break;
  1003.     }
  1004.   return (do_symval_forwarding (valcontents, buf));
  1005. }
  1006.  
  1007. Lisp_Object
  1008. find_symbol_value (Lisp_Object sym)
  1009. {
  1010.   Lisp_Object valcontents;
  1011.  
  1012.   CHECK_SYMBOL (sym, 0);
  1013.  
  1014.  retry:
  1015.   valcontents = XSYMBOL (sym)->value;
  1016.  
  1017.   if (!SYMBOL_VALUE_MAGIC_P (valcontents))
  1018.     return (valcontents);
  1019.  
  1020.   switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
  1021.     {
  1022.     case SYMVAL_VARALIAS:
  1023.       sym = follow_varalias_pointers (sym);
  1024.       /* presto change-o! */
  1025.       goto retry;
  1026.  
  1027.     case SYMVAL_BUFFER_LOCAL:
  1028.     case SYMVAL_SOME_BUFFER_LOCAL:
  1029.       {
  1030.     struct symbol_value_buffer_local *bfwd
  1031.       = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
  1032.     valcontents = swap_in_symval_forwarding (sym, bfwd);
  1033.     break;
  1034.       }
  1035.     default:
  1036.       break;
  1037.     }
  1038.  
  1039.   return do_symval_forwarding (valcontents, current_buffer);
  1040. }
  1041.  
  1042. DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
  1043.   "Return SYMBOL's value.  Error if that is void.")
  1044.   (sym)
  1045.      Lisp_Object sym;
  1046. {
  1047.   Lisp_Object val = find_symbol_value (sym);
  1048.  
  1049.   if (EQ (val, Qunbound))
  1050.     return Fsignal (Qvoid_variable, list1 (sym));
  1051.   else
  1052.     return val;
  1053. }
  1054.  
  1055. DEFUN ("set", Fset, Sset, 2, 2, 0,
  1056.   "Set SYMBOL's value to NEWVAL, and return NEWVAL.")
  1057.   (sym, newval)
  1058.      Lisp_Object sym, newval;
  1059. {
  1060.   REGISTER Lisp_Object valcontents;
  1061.  
  1062.   CHECK_SYMBOL (sym, 0);
  1063.  
  1064.  retry:
  1065.   reject_constant_symbols (sym, newval, 0);
  1066.   valcontents = XSYMBOL (sym)->value;
  1067.  
  1068.   if (SYMBOL_VALUE_MAGIC_P (valcontents))
  1069.     {
  1070.       switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
  1071.     {
  1072.     case SYMVAL_VARALIAS:
  1073.       sym = follow_varalias_pointers (sym);
  1074.       /* presto change-o! */
  1075.       goto retry;
  1076.  
  1077.     case SYMVAL_FIXNUM_FORWARD:
  1078.     case SYMVAL_BOOLEAN_FORWARD:
  1079.     case SYMVAL_OBJECT_FORWARD:
  1080.     case SYMVAL_DEFAULT_BUFFER_FORWARD:
  1081.       if (EQ (newval, Qunbound))
  1082.         signal_error (Qerror, 
  1083.               list2 (build_string ("Cannot makunbound"), sym));
  1084.       break;
  1085.  
  1086.     case SYMVAL_UNBOUND_MARKER:
  1087.       break;
  1088.  
  1089.     case SYMVAL_CURRENT_BUFFER_FORWARD:
  1090.       {
  1091.         CONST struct symbol_value_forward *fwd
  1092.           = XSYMBOL_VALUE_FORWARD (valcontents);
  1093.         int mask = XINT (*((Lisp_Object *)
  1094.                                symbol_value_forward_forward (fwd)));
  1095.         if (mask > 0)
  1096.           /* Setting this variable makes it buffer-local */
  1097.           current_buffer->local_var_flags |= mask;
  1098.         break;
  1099.       }
  1100.  
  1101.     case SYMVAL_BUFFER_LOCAL:
  1102.     case SYMVAL_SOME_BUFFER_LOCAL:
  1103.       {
  1104.         /* If we want to examine or set the value and BUFFER is current,
  1105.            we just examine or set CURRENT-VALUE. If BUFFER is not current,
  1106.            we store the current CURRENT-VALUE value into CURRENT-ALIST-
  1107.            ELEMENT, then find the appropriate alist element for the buffer
  1108.            now current and set up CURRENT-ALIST-ELEMENT.  Then we set
  1109.            CURRENT-VALUE out of that element, and store into BUFFER.
  1110.            
  1111.            If we are setting the variable and the current buffer does
  1112.            not have an alist entry for this variable, an alist entry is
  1113.            created.
  1114.            
  1115.            Note that CURRENT-VALUE can be a forwarding pointer.  Each time
  1116.            it is examined or set, forwarding must be done.
  1117.          */
  1118.         struct symbol_value_buffer_local *bfwd
  1119.           = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
  1120.         int some_buffer_local_p =
  1121.           (bfwd->magic.type == SYMVAL_SOME_BUFFER_LOCAL);
  1122.         /* What value are we caching right now?  */
  1123.         Lisp_Object aelt = bfwd->current_alist_element;
  1124.  
  1125.         if (!NILP (bfwd->current_buffer) &&
  1126.         current_buffer == XBUFFER (bfwd->current_buffer)
  1127.         && ((some_buffer_local_p)
  1128.             ? 1           /* doesn't automatically become local */
  1129.             : !NILP (aelt) /* already local */
  1130.             ))
  1131.           {
  1132.         /* Cache is valid */
  1133.         valcontents = bfwd->current_value;
  1134.           }
  1135.         else
  1136.           {
  1137.         /* If the current buffer is not the buffer whose binding is
  1138.            currently cached, or if it's a SYMVAL_BUFFER_LOCAL and
  1139.            we're looking at the default value, the cache is invalid; we
  1140.            need to write it out, and find the new CURRENT-ALIST-ELEMENT
  1141.            */
  1142.         
  1143.         /* Write out the cached value for the old buffer; copy it
  1144.            back to its alist element.  This works if the current
  1145.            buffer only sees the default value, too.  */
  1146.         /* Find the new value for CURRENT-ALIST-ELEMENT.  */
  1147.         aelt = swap_in_symval_forwarding_1 (sym, bfwd);
  1148.         if (NILP (aelt))
  1149.           {
  1150.             /* This buffer is still seeing the default value.  */
  1151.             if (!some_buffer_local_p)
  1152.               {
  1153.             /* If it's a SYMVAL_BUFFER_LOCAL, give this buffer a
  1154.                new assoc for a local value and set
  1155.                CURRENT-ALIST-ELEMENT to point to that.  */
  1156.             aelt = do_symval_forwarding (bfwd->current_value,
  1157.                              current_buffer);
  1158.             aelt = Fcons (sym, aelt);
  1159.             current_buffer->local_var_alist
  1160.               = Fcons (aelt, current_buffer->local_var_alist);
  1161.               }
  1162.             else
  1163.               {
  1164.             /* If the variable is a SYMVAL_SOME_BUFFER_LOCAL,
  1165.                we're currently seeing the default value. */
  1166.             ;
  1167.               }
  1168.           }
  1169.         /* Cache the new buffer's assoc in CURRENT-ALIST-ELEMENT.  */
  1170.         bfwd->current_alist_element = aelt;
  1171.         /* Set BUFFER, now that CURRENT-ALIST-ELEMENT is accurate.  */
  1172.         XSETBUFFER (bfwd->current_buffer, current_buffer);
  1173.         valcontents = bfwd->current_value;
  1174.           }
  1175.         break;
  1176.       }
  1177.     default:
  1178.       abort ();
  1179.     }
  1180.     }
  1181.   store_symval_forwarding (sym, valcontents, newval);
  1182.  
  1183.   return (newval);
  1184. }
  1185.  
  1186.  
  1187. /* Access or set a buffer-local symbol's default value.     */
  1188.  
  1189. /* Return the default value of SYM, but don't check for voidness.
  1190.    Return Qunbound if it is void.  */
  1191.  
  1192. static Lisp_Object
  1193. default_value (Lisp_Object sym)
  1194. {
  1195.   Lisp_Object valcontents;
  1196.  
  1197.   CHECK_SYMBOL (sym, 0);
  1198.  
  1199.  retry:
  1200.   valcontents = XSYMBOL (sym)->value;
  1201.  
  1202.   if (!SYMBOL_VALUE_MAGIC_P (valcontents))
  1203.     return (valcontents);
  1204.     
  1205.   switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
  1206.     {
  1207.     case SYMVAL_VARALIAS:
  1208.       sym = follow_varalias_pointers (sym);
  1209.       /* presto change-o! */
  1210.       goto retry;
  1211.  
  1212.     case SYMVAL_UNBOUND_MARKER:
  1213.       return valcontents;
  1214.  
  1215.     case SYMVAL_CURRENT_BUFFER_FORWARD:
  1216.       {
  1217.     CONST struct symbol_value_forward *fwd
  1218.       = XSYMBOL_VALUE_FORWARD (valcontents);
  1219.     return (*((Lisp_Object *)((char *) XBUFFER (Vbuffer_defaults)
  1220.                   + ((char *)symbol_value_forward_forward (fwd)
  1221.                      - (char *)&buffer_local_flags))));
  1222.       }
  1223.     case SYMVAL_BUFFER_LOCAL:
  1224.     case SYMVAL_SOME_BUFFER_LOCAL:
  1225.       {
  1226.     struct symbol_value_buffer_local *bfwd = 
  1227.       XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
  1228.  
  1229.     /* Handle user-created local variables.     */
  1230.     /* If var is set up for a buffer that lacks a local value for it,
  1231.        the current value is nominally the default value.
  1232.        But the current value slot may be more up to date, since
  1233.        ordinary setq stores just that slot.     So use that.  */
  1234.     if (NILP (bfwd->current_alist_element))
  1235.       return (do_symval_forwarding (bfwd->current_value, current_buffer));
  1236.     else
  1237.       return (bfwd->default_value);
  1238.       }
  1239.     default:
  1240.       /* For other variables, get the current value.    */
  1241.       return (do_symval_forwarding (valcontents, current_buffer));
  1242.     }
  1243.   return Qnil;    /* suppress compiler warning */
  1244. }
  1245.  
  1246. DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
  1247.   "Return T if SYMBOL has a non-void default value.\n\
  1248. This is the value that is seen in buffers that do not have their own values\n\
  1249. for this variable.")
  1250.   (sym)
  1251.      Lisp_Object sym;
  1252. {
  1253.   Lisp_Object value;
  1254.  
  1255.   value = default_value (sym);
  1256.   return (EQ (value, Qunbound) ? Qnil : Qt);
  1257. }
  1258.  
  1259. DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
  1260.   "Return SYMBOL's default value.\n\
  1261. This is the value that is seen in buffers that do not have their own values\n\
  1262. for this variable.  The default value is meaningful for variables with\n\
  1263. local bindings in certain buffers.")
  1264.   (sym)
  1265.      Lisp_Object sym;
  1266. {
  1267.   Lisp_Object value;
  1268.  
  1269.   value = default_value (sym);
  1270.   if (EQ (value, Qunbound))
  1271.     return Fsignal (Qvoid_variable, list1 (sym));
  1272.   return value;
  1273. }
  1274.  
  1275. DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
  1276.   "Set SYMBOL's default value to VAL.  SYMBOL and VAL are evaluated.\n\
  1277. The default value is seen in buffers that do not have their own values\n\
  1278. for this variable.")
  1279.   (sym, value)
  1280.      Lisp_Object sym, value;
  1281. {
  1282.   Lisp_Object valcontents;
  1283.  
  1284.   CHECK_SYMBOL (sym, 0);
  1285.  
  1286.  retry:
  1287.   valcontents = XSYMBOL (sym)->value;
  1288.  
  1289.   if (!SYMBOL_VALUE_MAGIC_P (valcontents))
  1290.     return Fset (sym, value);
  1291.  
  1292.   switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
  1293.     {
  1294.     case SYMVAL_VARALIAS:
  1295.       sym = follow_varalias_pointers (sym);
  1296.       /* presto change-o! */
  1297.       goto retry;
  1298.  
  1299.     case SYMVAL_CURRENT_BUFFER_FORWARD:
  1300.       set_default_buffer_slot_variable (sym, value);
  1301.       return (value);
  1302.  
  1303.     case SYMVAL_BUFFER_LOCAL:
  1304.     case SYMVAL_SOME_BUFFER_LOCAL:
  1305.       {
  1306.     /* Store new value into the DEFAULT-VALUE slot */
  1307.     struct symbol_value_buffer_local *bfwd
  1308.       = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
  1309.  
  1310.     bfwd->default_value = value;
  1311.     /* If current-buffer doesn't shadow default_value,
  1312.      *  we must set the CURRENT-VALUE slot too */
  1313.     if (NILP (bfwd->current_alist_element))
  1314.       store_symval_forwarding (sym, bfwd->current_value, value);
  1315.     return (value);
  1316.       }
  1317.  
  1318.     default:
  1319.       return Fset (sym, value);
  1320.     }
  1321.   return Qnil;    /* suppress compiler warning */
  1322. }
  1323.  
  1324. DEFUN ("setq-default", Fsetq_default, Ssetq_default, 2, UNEVALLED, 0,
  1325.        "Set the default value of variable VAR to VALUE.\n\
  1326. VAR, the variable name, is literal (not evaluated);\n\
  1327. VALUE is an expression and it is evaluated.\n\
  1328. The default value of a variable is seen in buffers\n\
  1329. that do not have their own values for the variable.\n\
  1330. \n\
  1331. More generally, you can use multiple variables and values, as in\n\
  1332.   (setq-default SYM VALUE SYM VALUE...)\n\
  1333. This sets each SYM's default value to the corresponding VALUE.\n\
  1334. The VALUE for the Nth SYM can refer to the new default values\n\
  1335. of previous SYMs.")
  1336.   (args)
  1337.      Lisp_Object args;
  1338. {
  1339.   /* This function can GC */
  1340.   Lisp_Object args_left;
  1341.   Lisp_Object val, sym;
  1342.   struct gcpro gcpro1;
  1343.  
  1344.   if (NILP (args))
  1345.     return Qnil;
  1346.  
  1347.   args_left = args;
  1348.   GCPRO1 (args);
  1349.  
  1350.   do
  1351.     {
  1352.       val = Feval (Fcar (Fcdr (args_left)));
  1353.       sym = Fcar (args_left);
  1354.       Fset_default (sym, val);
  1355.       args_left = Fcdr (Fcdr (args_left));
  1356.     }
  1357.   while (!NILP (args_left));
  1358.  
  1359.   UNGCPRO;
  1360.   return val;
  1361. }
  1362.  
  1363. /* Lisp functions for creating and removing buffer-local variables.  */
  1364.  
  1365. DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
  1366.        Smake_variable_buffer_local,
  1367.   1, 1, "vMake Variable Buffer Local: ",
  1368.   "Make VARIABLE have a separate value for each buffer.\n\
  1369. At any time, the value for the current buffer is in effect.\n\
  1370. There is also a default value which is seen in any buffer which has not yet\n\
  1371. set its own value.\n\
  1372. Using `set' or `setq' to set the variable causes it to have a separate value\n\
  1373. for the current buffer if it was previously using the default value.\n\
  1374. The function `default-value' gets the default value and `set-default'\n\
  1375. sets it.")
  1376.   (variable)
  1377.      Lisp_Object variable;
  1378. {
  1379.   Lisp_Object valcontents;
  1380.  
  1381.   CHECK_SYMBOL (variable, 0);
  1382.  
  1383.  retry:
  1384.   verify_ok_for_buffer_local (variable);
  1385.  
  1386.   valcontents = XSYMBOL (variable)->value;
  1387.  
  1388.   if (SYMBOL_VALUE_MAGIC_P (valcontents))
  1389.     {
  1390.       switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
  1391.     {
  1392.     case SYMVAL_VARALIAS:
  1393.       variable = follow_varalias_pointers (variable);
  1394.       /* presto change-o! */
  1395.       goto retry;
  1396.  
  1397.     case SYMVAL_FIXNUM_FORWARD:
  1398.     case SYMVAL_BOOLEAN_FORWARD:
  1399.     case SYMVAL_OBJECT_FORWARD:
  1400.     case SYMVAL_UNBOUND_MARKER:
  1401.       break;
  1402.  
  1403.     case SYMVAL_CURRENT_BUFFER_FORWARD:
  1404.     case SYMVAL_BUFFER_LOCAL:
  1405.       /* Already per-each-buffer */
  1406.       return (variable);
  1407.  
  1408.     case SYMVAL_SOME_BUFFER_LOCAL:
  1409.       /* Transmogrify */
  1410.       XSYMBOL_VALUE_BUFFER_LOCAL (valcontents)->magic.type =
  1411.         SYMVAL_BUFFER_LOCAL;
  1412.       return (variable);
  1413.  
  1414.     default:
  1415.       abort ();
  1416.     }
  1417.     }
  1418.   
  1419.   {
  1420.     struct symbol_value_buffer_local *bfwd
  1421.       = alloc_lcrecord (sizeof (struct symbol_value_buffer_local),
  1422.             lrecord_symbol_value_buffer_local);
  1423.     bfwd->magic.type = SYMVAL_BUFFER_LOCAL;
  1424.  
  1425.     bfwd->default_value = find_symbol_value (variable);
  1426.     bfwd->current_value = valcontents;
  1427.     bfwd->current_alist_element = Qnil;
  1428.     bfwd->current_buffer = Fcurrent_buffer ();
  1429.     XSETSYMBOL_VALUE_MAGIC (XSYMBOL (variable)->value, bfwd);
  1430. #if 1                /* #### Yuck!   FSFmacs bug-compatibility*/
  1431.     /* This sets the default-value of any make-variable-buffer-local to nil.
  1432.        That just sucks.     User can just use setq-default to effect that,
  1433.        but there's no way to do makunbound-default to undo this lossage. */
  1434.     if (EQ (valcontents, Qunbound))
  1435.       bfwd->default_value = Qnil;
  1436. #endif
  1437. #if 0                /* #### Yuck! */
  1438.     /* This sets the value to nil in this buffer.
  1439.        User could use (setq variable nil) to do this.
  1440.        It isn't as egregious to do this automatically
  1441.        as it is to do so to the default-value, but it's
  1442.        still really dubious. */
  1443.     if (EQ (valcontents, Qunbound))
  1444.       Fset (variable, Qnil);
  1445. #endif
  1446.     return (variable);
  1447.   }
  1448. }
  1449.  
  1450. DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
  1451.   1, 1, "vMake Local Variable: ",
  1452.   "Make VARIABLE have a separate value in the current buffer.\n\
  1453. Other buffers will continue to share a common default value.\n\
  1454. (The buffer-local value of VARIABLE starts out as the same value\n\
  1455. VARIABLE previously had.  If VARIABLE was void, it remains void.)\n\
  1456. See also `make-variable-buffer-local'.\n\
  1457. \n\
  1458. If the variable is already arranged to become local when set,\n\
  1459. this function causes a local value to exist for this buffer,\n\
  1460. just as if the variable were set.")
  1461.   (variable)
  1462.      Lisp_Object variable;
  1463. {
  1464.   Lisp_Object valcontents;
  1465.   struct symbol_value_buffer_local *bfwd;
  1466.  
  1467.   CHECK_SYMBOL (variable, 0);
  1468.  
  1469.  retry:
  1470.   verify_ok_for_buffer_local (variable);
  1471.  
  1472.   valcontents = XSYMBOL (variable)->value;
  1473.  
  1474.   if (SYMBOL_VALUE_MAGIC_P (valcontents))
  1475.     {
  1476.       switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
  1477.     {
  1478.     case SYMVAL_VARALIAS:
  1479.       variable = follow_varalias_pointers (variable);
  1480.       /* presto change-o! */
  1481.       goto retry;
  1482.  
  1483.     case SYMVAL_FIXNUM_FORWARD:
  1484.     case SYMVAL_BOOLEAN_FORWARD:
  1485.     case SYMVAL_OBJECT_FORWARD:
  1486.     case SYMVAL_UNBOUND_MARKER:
  1487.       break;
  1488.  
  1489.     case SYMVAL_BUFFER_LOCAL:
  1490.     case SYMVAL_CURRENT_BUFFER_FORWARD:
  1491.       {
  1492.         /* Make sure the symbol has a local value in this particular
  1493.            buffer, by setting it to the same value it already has.    */
  1494.         Fset (variable, find_symbol_value (variable));
  1495.         return (variable);
  1496.       }
  1497.  
  1498.     case SYMVAL_SOME_BUFFER_LOCAL:
  1499.       {
  1500.         if (!NILP (buffer_local_alist_element (current_buffer,
  1501.                            variable,
  1502.                            (XSYMBOL_VALUE_BUFFER_LOCAL
  1503.                             (valcontents)))))
  1504.           goto already_local_to_current_buffer;
  1505.         else
  1506.           goto already_local_to_some_other_buffer;
  1507.       }
  1508.  
  1509.     default:
  1510.       abort ();
  1511.     }
  1512.     }
  1513.  
  1514.   /* Make sure variable is set up to hold per-buffer values */
  1515.   bfwd = alloc_lcrecord (sizeof (struct symbol_value_buffer_local),
  1516.              lrecord_symbol_value_buffer_local);
  1517.   bfwd->magic.type = SYMVAL_SOME_BUFFER_LOCAL;
  1518.  
  1519.   bfwd->current_buffer = Qnil;
  1520.   bfwd->current_alist_element = Qnil;
  1521.   bfwd->current_value = valcontents;
  1522.   bfwd->default_value = do_symval_forwarding (valcontents, current_buffer);
  1523.  
  1524. #if 0
  1525.   if (EQ (bfwd->default_value, Qunbound))
  1526.     bfwd->default_value = Qnil; /* Yuck! */
  1527. #endif
  1528.  
  1529.   XSETSYMBOL_VALUE_MAGIC (valcontents, bfwd);
  1530.   XSYMBOL (variable)->value = valcontents;
  1531.  
  1532.  already_local_to_some_other_buffer:
  1533.  
  1534.   /* Make sure this buffer has its own value of variable */
  1535.   bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
  1536.  
  1537.   if (EQ (bfwd->default_value, Qunbound))
  1538.   {
  1539.     /* If default value is unbound, set local value to nil. */
  1540.     XSETBUFFER (bfwd->current_buffer, current_buffer);
  1541.     bfwd->current_alist_element = Fcons (variable, Qnil);
  1542.     current_buffer->local_var_alist = Fcons (bfwd->current_alist_element,
  1543.                          current_buffer->local_var_alist);
  1544.     store_symval_forwarding (variable, bfwd->current_value, Qnil);
  1545.     return (variable);
  1546.   }
  1547.  
  1548.   current_buffer->local_var_alist
  1549.     = Fcons (Fcons (variable, bfwd->default_value),
  1550.          current_buffer->local_var_alist);
  1551.  
  1552.   /* Make sure symbol does not think it is set up for this buffer;
  1553.      force it to look once again for this buffer's value */
  1554.   if (!NILP (bfwd->current_buffer) &&
  1555.       current_buffer == XBUFFER (bfwd->current_buffer))
  1556.     bfwd->current_buffer = Qnil;
  1557.  
  1558.  already_local_to_current_buffer:
  1559.  
  1560.   /* If the symbol forwards into a C variable, then swap in the
  1561.      variable for this buffer immediately.  If C code modifies the
  1562.      variable before we swap in, then that new value will clobber the
  1563.      default value the next time we swap.  */
  1564.   bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
  1565.   if (SYMBOL_VALUE_MAGIC_P (bfwd->current_value))
  1566.     {
  1567.       switch (XSYMBOL_VALUE_MAGIC_TYPE (bfwd->current_value))
  1568.     {
  1569.     case SYMVAL_FIXNUM_FORWARD:
  1570.     case SYMVAL_BOOLEAN_FORWARD:
  1571.     case SYMVAL_OBJECT_FORWARD:
  1572.     case SYMVAL_DEFAULT_BUFFER_FORWARD:
  1573.       swap_in_symval_forwarding (variable, bfwd);
  1574.       break;
  1575.  
  1576.     case SYMVAL_UNBOUND_MARKER:
  1577.     case SYMVAL_CURRENT_BUFFER_FORWARD:
  1578.       break;
  1579.  
  1580.     default:
  1581.       abort ();
  1582.     }
  1583.     }
  1584.  
  1585.   return (variable);
  1586. }
  1587.  
  1588. DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
  1589.   1, 1, "vKill Local Variable: ",
  1590.   "Make VARIABLE no longer have a separate value in the current buffer.\n\
  1591. From now on the default value will apply in this buffer.")
  1592.   (variable)
  1593.      Lisp_Object variable;
  1594. {
  1595.   Lisp_Object valcontents;
  1596.  
  1597.   CHECK_SYMBOL (variable, 0);
  1598.  
  1599.  retry:
  1600.   valcontents = XSYMBOL (variable)->value;
  1601.  
  1602.   if (!SYMBOL_VALUE_MAGIC_P (valcontents))
  1603.     return (variable);
  1604.  
  1605.   switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
  1606.     {
  1607.     case SYMVAL_VARALIAS:
  1608.       variable = follow_varalias_pointers (variable);
  1609.       /* presto change-o! */
  1610.       goto retry;
  1611.  
  1612.     case SYMVAL_CURRENT_BUFFER_FORWARD:
  1613.       {
  1614.     CONST struct symbol_value_forward *fwd
  1615.       = XSYMBOL_VALUE_FORWARD (valcontents);
  1616.     int offset = ((char *) symbol_value_forward_forward (fwd) 
  1617.                    - (char *) &buffer_local_flags);
  1618.     int mask =
  1619.       XINT (*((Lisp_Object *) symbol_value_forward_forward (fwd)));
  1620.  
  1621.     if (mask > 0)
  1622.       {
  1623.         int (*magicfun) (Lisp_Object sym, Lisp_Object *val,
  1624.                  struct buffer *buf, int flags) =
  1625.                    symbol_value_forward_magicfun (fwd);
  1626.         Lisp_Object oldval = * (Lisp_Object *)
  1627.           (offset + (char *) XBUFFER (Vbuffer_defaults));
  1628.         if (magicfun)
  1629.           (magicfun) (variable, &oldval, current_buffer, 0);
  1630.         *(Lisp_Object *) (offset + (char *) current_buffer)
  1631.           = oldval;
  1632.         current_buffer->local_var_flags &= ~mask;
  1633.       }
  1634.     return (variable);
  1635.       }
  1636.  
  1637.     case SYMVAL_BUFFER_LOCAL:
  1638.     case SYMVAL_SOME_BUFFER_LOCAL:
  1639.       {
  1640.     /* Get rid of this buffer's alist element, if any */
  1641.     struct symbol_value_buffer_local *bfwd
  1642.       = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
  1643.     Lisp_Object alist = current_buffer->local_var_alist;
  1644.     Lisp_Object alist_element
  1645.       = buffer_local_alist_element (current_buffer, variable, bfwd);
  1646.  
  1647.     if (!NILP (alist_element))
  1648.       current_buffer->local_var_alist = Fdelq (alist_element, alist);
  1649.  
  1650.     /* Make sure symbol does not think it is set up for this buffer;
  1651.        force it to look once again for this buffer's value */
  1652.     if (!NILP (bfwd->current_buffer) &&
  1653.         current_buffer == XBUFFER (bfwd->current_buffer))
  1654.       bfwd->current_buffer = Qnil;
  1655.  
  1656.     /* In case it's a C variable, flush it out. */
  1657.     swap_in_symval_forwarding (variable, bfwd);
  1658.       }
  1659.       return (variable);
  1660.  
  1661.     default:
  1662.       return (variable);
  1663.     }
  1664.   return Qnil;    /* suppress compiler warning */
  1665. }
  1666.  
  1667. /* Used by specbind to determine what effects it might have.  Returns:
  1668.  *   0 if symbol isn't buffer-local, and wouldn't be after it is set
  1669.  *  <0 if symbol isn't presently buffer-local, but set would make it so
  1670.  *  >0 if symbol is presently buffer-local
  1671.  */
  1672. int
  1673. symbol_value_buffer_local_info (Lisp_Object symbol, struct buffer *buffer)
  1674. {
  1675.   Lisp_Object valcontents;
  1676.  
  1677.  retry:
  1678.   valcontents = XSYMBOL (symbol)->value;
  1679.   
  1680.   if (SYMBOL_VALUE_MAGIC_P (valcontents))
  1681.     {
  1682.       switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
  1683.     {
  1684.     case SYMVAL_VARALIAS:
  1685.       symbol = follow_varalias_pointers (symbol);
  1686.       /* presto change-o! */
  1687.       goto retry;
  1688.  
  1689.     case SYMVAL_CURRENT_BUFFER_FORWARD:
  1690.       {
  1691.         CONST struct symbol_value_forward *fwd
  1692.           = XSYMBOL_VALUE_FORWARD (valcontents);
  1693.         int mask = XINT (*((Lisp_Object *)
  1694.                    symbol_value_forward_forward (fwd)));
  1695.         if ((mask <= 0) || (buffer && (buffer->local_var_flags & mask)))
  1696.           /* Already buffer-local */
  1697.           return (1);
  1698.         else
  1699.           /* Would be buffer-local after set */
  1700.           return (-1);
  1701.       }
  1702.     case SYMVAL_BUFFER_LOCAL:
  1703.     case SYMVAL_SOME_BUFFER_LOCAL:
  1704.       {
  1705.         struct symbol_value_buffer_local *bfwd
  1706.           = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
  1707.         if (buffer
  1708.         && !NILP (buffer_local_alist_element (buffer, symbol, bfwd)))
  1709.           return (1);
  1710.         else
  1711.           return ((bfwd->magic.type == SYMVAL_BUFFER_LOCAL)
  1712.               ? -1     /* Automatically becomes local when set */
  1713.               : 0);
  1714.       }
  1715.     default:
  1716.       return (0);
  1717.     }
  1718.     }
  1719.   return (0);
  1720. }
  1721.  
  1722.  
  1723. DEFUN ("symbol-value-in-buffer", Fsymbol_value_in_buffer, Ssymbol_value_in_buffer, 3, 3, 0,
  1724.   "Return the value of SYMBOL in BUFFER, or UNBOUND-VALUE if it is unbound.")
  1725.   (symbol, buffer, unbound_value)
  1726.      Lisp_Object symbol, buffer, unbound_value;
  1727. {
  1728.   Lisp_Object value;
  1729.   CHECK_SYMBOL (symbol, 0);
  1730.   CHECK_BUFFER (buffer, 0);
  1731.   value = symbol_value_in_buffer (symbol, buffer);
  1732.   if (EQ (value, Qunbound))
  1733.     return (unbound_value);
  1734.   else
  1735.     return (value);
  1736. }
  1737.  
  1738. DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p, 2, 3, 0,
  1739.   "Return t if SYMBOL's value is local to BUFFER.\n\
  1740. If optional third arg AFTER-SET is true, return t if SYMBOL would be\n\
  1741. buffer-local after it is set, regardless of whether it is so presently.")
  1742.   (symbol, buffer, after_set)
  1743.      Lisp_Object symbol, buffer, after_set;
  1744. {
  1745.   int local_info;
  1746.   
  1747.   CHECK_SYMBOL (symbol, 0);
  1748.   if (!NILP (buffer))
  1749.     {
  1750.       buffer = get_buffer (buffer, 1);
  1751.       local_info = symbol_value_buffer_local_info (symbol, XBUFFER (buffer));
  1752.     }
  1753.   else
  1754.     {
  1755.       local_info = symbol_value_buffer_local_info (symbol, 0);
  1756.     }
  1757.   
  1758.   if (NILP (after_set))
  1759.     return ((local_info > 0) ? Qt : Qnil);
  1760.   else
  1761.     return ((local_info != 0) ? Qt : Qnil);
  1762. }
  1763.  
  1764.  
  1765. /* Lisp functions for working with variable aliases.  */
  1766.  
  1767. DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 2, 0,
  1768.   "Define a variable as an alias for another variable.\n\
  1769. Thenceforth, any operations performed on VARIABLE will actually be\n\
  1770. performed on ALIAS.  Both VARIABLE and ALIAS should be symbols.\n\
  1771. If ALIAS is nil, remove any aliases for VARIABLE.\n\
  1772. ALIAS can itself be aliased, and the chain of variable aliases\n\
  1773. will be followed appropriately.\n\
  1774. If VARIABLE already has a value, this value will be shadowed\n\
  1775. until the alias is removed, at which point it will be restored.\n\
  1776. Currently VARIABLE cannot be a built-in variable, a variable that\n\
  1777. has a buffer-local value in any buffer, or the symbols nil or t.")
  1778.   (variable, alias)
  1779.     Lisp_Object variable, alias;
  1780. {
  1781.   struct symbol_value_varalias *bfwd;
  1782.   Lisp_Object valcontents;
  1783.  
  1784.   CHECK_SYMBOL (variable, 0);
  1785.   reject_constant_symbols (variable, Qunbound, 0);
  1786.  
  1787.   valcontents = XSYMBOL (variable)->value;
  1788.  
  1789.   if (NILP (alias))
  1790.     {
  1791.       if (SYMBOL_VALUE_VARALIAS_P (valcontents))
  1792.     {
  1793.       XSYMBOL (variable)->value =
  1794.         symbol_value_varalias_shadowed
  1795.           (XSYMBOL_VALUE_VARALIAS (valcontents));
  1796.     }
  1797.       return Qnil;
  1798.     }
  1799.       
  1800.   CHECK_SYMBOL (alias, 1);
  1801.   if (SYMBOL_VALUE_MAGIC_P (valcontents) && !EQ (valcontents, Qunbound))
  1802.     signal_simple_error ("Variable cannot be aliased", variable);
  1803.  
  1804.   bfwd = alloc_lcrecord (sizeof (struct symbol_value_varalias),
  1805.              lrecord_symbol_value_varalias);
  1806.   bfwd->magic.type = SYMVAL_VARALIAS;
  1807.   bfwd->aliasee = alias;
  1808.   bfwd->shadowed = valcontents;
  1809.   
  1810.   XSETSYMBOL_VALUE_MAGIC (valcontents, bfwd);
  1811.   XSYMBOL (variable)->value = valcontents;
  1812.   return Qnil;
  1813. }
  1814.  
  1815. DEFUN ("variable-alias", Fvariable_alias, Svariable_alias, 1, 1, 0,
  1816.   "If VARIABLE is aliased to another variable, return that variable.\n\
  1817. VARIABLE should be a symbol.  If VARIABLE is not aliased, return nil.\n\
  1818. Variable aliases are created with `defvaralias'.  See also\n\
  1819. `indirect-variable'.")
  1820.   (variable)
  1821.     Lisp_Object variable;
  1822. {
  1823.   Lisp_Object valcontents;
  1824.  
  1825.   CHECK_SYMBOL (variable, 0);
  1826.  
  1827.   valcontents = XSYMBOL (variable)->value;
  1828.  
  1829.   if (SYMBOL_VALUE_VARALIAS_P (valcontents))
  1830.     return symbol_value_varalias_aliasee
  1831.       (XSYMBOL_VALUE_VARALIAS (valcontents));
  1832.   else
  1833.     return Qnil;
  1834. }
  1835.  
  1836. DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
  1837.   "Return the variable at the end of OBJECT's variable-alias chain.\n\
  1838. If OBJECT is a symbol, follow all variable aliases and return\n\
  1839. the final (non-aliased) symbol.  Variable aliases are created with\n\
  1840. the function `defvaralias'.\n\
  1841. If OBJECT is not a symbol, just return it.\n\
  1842. Signal a cyclic-variable-indirection error if there is a loop in the\n\
  1843. variable chain of symbols.")
  1844.   (object)
  1845.     Lisp_Object object;
  1846. {
  1847.   if (!SYMBOLP (object))
  1848.     return object;
  1849.   return follow_varalias_pointers (object);
  1850. }
  1851.  
  1852.  
  1853. /************************************************************************/
  1854. /*                            initialization                            */
  1855. /************************************************************************/
  1856.  
  1857. /* A dumped XEmacs image has a lot more than 1511 symbols.  Last
  1858.    estimate was that there were actually around 6300.  So let's try
  1859.    making this bigger and see if we get better hashing behavior. */
  1860. #define OBARRAY_SIZE 16411
  1861.  
  1862. #ifndef Qzero
  1863. Lisp_Object Qzero;
  1864. #endif
  1865.  
  1866. /* some losing systems can't have static vars at function scope... */
  1867. static struct symbol_value_magic guts_of_unbound_marker =
  1868.   { { { lrecord_symbol_value_forward }, 0, 69}, SYMVAL_UNBOUND_MARKER };
  1869.  
  1870. void
  1871. init_symbols_once_early (void)
  1872. {
  1873.   Qnil = Fmake_symbol (make_pure_pname ((Bufbyte *) "nil", 3, 1));
  1874.   /* Bootstrapping problem: Qnil isn't set when make_pure_pname is
  1875.      called the first time. */
  1876.   XSYMBOL (Qnil)->name->plist = Qnil;
  1877.   XSYMBOL (Qnil)->value = Qnil; /* Nihil ex nihil */
  1878.   XSYMBOL (Qnil)->plist = Qnil;
  1879.   
  1880. #ifndef Qzero
  1881.   Qzero = make_number (0);    /* Only used if Lisp_Object is a union type */
  1882. #endif
  1883.   
  1884.   Vobarray = make_vector (OBARRAY_SIZE, Qzero);
  1885.   initial_obarray = Vobarray;
  1886.   staticpro (&initial_obarray);
  1887.   /* Intern nil in the obarray */
  1888.   {
  1889.     /* These locals are to kludge around a pyramid compiler bug. */
  1890.     int hash;
  1891.     Lisp_Object *tem;
  1892.     
  1893.     hash = hash_string (string_data (XSYMBOL (Qnil)->name), 3);
  1894.     /* Separate statement here to avoid VAXC bug. */
  1895.     hash %= OBARRAY_SIZE;
  1896.     tem = &vector_data (XVECTOR (Vobarray))[hash];
  1897.     *tem = Qnil;
  1898.   }
  1899.   
  1900.   {
  1901.     /* Required to get around a GCC syntax error on certain
  1902.        architectures */
  1903.     struct symbol_value_magic *tem = &guts_of_unbound_marker;
  1904.     
  1905.     XSETSYMBOL_VALUE_MAGIC (Qunbound, tem);
  1906.   }
  1907.   if ((CONST void *) XPNTR (Qunbound) !=
  1908.       (CONST void *)&guts_of_unbound_marker)
  1909.     {
  1910.       /* This might happen on DATA_SEG_BITS machines. */
  1911.       /* abort (); */
  1912.       /* Can't represent a pointer to constant C data using a Lisp_Object.
  1913.      So heap-allocate it. */
  1914.       struct symbol_value_magic *urk = xmalloc (sizeof (*urk));
  1915.       memcpy (urk, &guts_of_unbound_marker, sizeof (*urk));
  1916.       XSETSYMBOL_VALUE_MAGIC (Qunbound, urk);
  1917.     }
  1918.   
  1919.   XSYMBOL (Qnil)->function = Qunbound;
  1920.   
  1921.   defsymbol (&Qt, "t");
  1922.   XSYMBOL (Qt)->value = Qt;    /* Veritas aetera */
  1923.   Vquit_flag = Qnil;
  1924. }
  1925.  
  1926. void
  1927. defsymbol (Lisp_Object *location, CONST char *name)
  1928. {
  1929.   *location = Fintern (make_pure_pname ((Bufbyte *) name, strlen (name), 1),
  1930.                Qnil);
  1931.   staticpro (location);
  1932. }
  1933.  
  1934. void
  1935. defkeyword (Lisp_Object *location, CONST char *name)
  1936. {
  1937.   defsymbol (location, name);
  1938.   Fset (*location, *location);
  1939. }
  1940.  
  1941. void
  1942. defsubr (struct Lisp_Subr *subr)
  1943. {
  1944.   Lisp_Object sym = intern (subr_name (subr));
  1945.  
  1946.   /* Check that nobody spazzed */
  1947.   if (subr->max_args != MANY && subr->max_args != UNEVALLED)
  1948.   {
  1949.     if (subr->max_args > SUBR_MAX_ARGS /* Need to fix eval.c if so */
  1950.     || subr->max_args < subr->min_args)
  1951.       abort ();
  1952.   }
  1953.   if (subr->min_args < 0 || subr->min_args > SUBR_MAX_ARGS)
  1954.     abort ();
  1955.  
  1956.   if (!EQ (XSYMBOL (sym)->function, Qunbound)) abort ();
  1957.  
  1958.   XSETSUBR (XSYMBOL (sym)->function, subr);
  1959. }
  1960.  
  1961. void
  1962. deferror (Lisp_Object *symbol, CONST char *name, CONST char *message,
  1963.       int error_type)
  1964. {
  1965.   defsymbol (symbol, name);
  1966.   /* There should probably be a better way of dealing with this. */
  1967.   pure_put (*symbol, Qerror_conditions,
  1968.         error_type == 0 ? list1 (*symbol) :
  1969.         error_type == 1 ? list2 (*symbol, Qerror) :
  1970.         error_type == 2 ? list3 (*symbol, Qarith_error, Qerror) :
  1971.         error_type == 3 ? list4 (*symbol, Qdomain_error, Qarith_error,
  1972.                      Qerror) :
  1973.         error_type == 4 ? list3 (*symbol, Qfile_error, Qerror) :
  1974.         (abort (), Qnil));
  1975.   /* NOT build_translated_string ().  This function is called at load time
  1976.      and the string needs to get translated at run time.  (This happens
  1977.      in the function (display-error) in cmdloop.el.) */
  1978.   pure_put (*symbol, Qerror_message, build_string (message));
  1979. }
  1980.  
  1981. void
  1982. syms_of_symbols (void)
  1983. {
  1984.   /* some basic symbols used in Ffset and such */
  1985.   defsymbol (&Qvariable_documentation, "variable-documentation");
  1986.   defsymbol (&Qvariable_domain, "variable-domain");    /* I18N3 */
  1987.   defsymbol (&Qad_advice_info, "ad-advice-info");
  1988.   defsymbol (&Qad_activate, "ad-activate");
  1989.  
  1990.   defsubr (&Sintern);
  1991.   defsubr (&Sintern_soft);
  1992.   defsubr (&Smapatoms);
  1993.   defsubr (&Sapropos_internal);
  1994.  
  1995.   defsubr (&Ssymbol_function);
  1996.   defsubr (&Ssymbol_plist);
  1997.   defsubr (&Ssymbol_name);
  1998.   defsubr (&Smakunbound);
  1999.   defsubr (&Sfmakunbound);
  2000.   defsubr (&Sboundp);
  2001.   defsubr (&Sglobally_boundp);
  2002.   defsubr (&Sfboundp);
  2003.   defsubr (&Sfset);
  2004.   defsubr (&Sdefine_function);
  2005.   defsubr (&Ssetplist);
  2006.   defsubr (&Ssymbol_value_in_buffer);
  2007.   defsubr (&Ssymbol_value);
  2008.   defsubr (&Sset);
  2009.   defsubr (&Sdefault_boundp);
  2010.   defsubr (&Sdefault_value);
  2011.   defsubr (&Sset_default);
  2012.   defsubr (&Ssetq_default);
  2013.   defsubr (&Smake_variable_buffer_local);
  2014.   defsubr (&Smake_local_variable);
  2015.   defsubr (&Skill_local_variable);
  2016.   defsubr (&Slocal_variable_p);
  2017.   defsubr (&Sdefvaralias);
  2018.   defsubr (&Svariable_alias);
  2019.   defsubr (&Sindirect_variable);
  2020. }
  2021.  
  2022. /* Create and initialize a variable whose value is forwarded to C data */
  2023. void
  2024. defvar_mumble (CONST char *namestring, 
  2025.            CONST void *magic, int sizeof_magic)
  2026. {
  2027.   Lisp_Object kludge;
  2028.   Lisp_Object sym = Fintern (make_pure_pname ((Bufbyte *) namestring,
  2029.                           strlen (namestring),
  2030.                           1),
  2031.                  Qnil);
  2032.  
  2033.   /* Check that magic points somewhere we can represent as a Lisp pointer */
  2034.   XSETOBJ (kludge, Lisp_Record, magic);
  2035.   if (magic != (CONST void *) XPNTR (kludge))
  2036.   {
  2037.     /* This might happen on DATA_SEG_BITS machines. */
  2038.     /* abort (); */
  2039.     /* Copy it to somewhere which is representable. */
  2040.     void *f = xmalloc (sizeof_magic);
  2041.     memcpy (f, magic, sizeof_magic);
  2042.     XSETOBJ (XSYMBOL (sym)->value, Lisp_Record, f);
  2043.   }
  2044.   else
  2045.     XSETOBJ (XSYMBOL (sym)->value, Lisp_Record, magic);
  2046. }
  2047.  
  2048. void
  2049. vars_of_symbols (void)
  2050. {
  2051.   DEFVAR_LISP ("obarray", &Vobarray,
  2052.     "Symbol table for use by `intern' and `read'.\n\
  2053. It is a vector whose length ought to be prime for best results.\n\
  2054. The vector's contents don't make sense if examined from Lisp programs;\n\
  2055. to find all the symbols in an obarray, use `mapatoms'.");
  2056.   /* obarray has been initialized long before */
  2057. }
  2058.